package com.cskaoyan.javase.thread._2api;

import java.util.concurrent.TimeUnit;

/**
 * 线程控制API
 * 线程的名字,线程都是有名字的,默认情况下,会根据启动的先后顺序给线程一个默认的名字
 * 从"Thread-0","Thread-1","Thread-2"...往后进行命名
 *
 * 启动main方法,是启动主线程,主线程也是有名字的,而且主线程的名字也是可以改的
 * 如何获取当前正在执行的线程呢?
 * public static native Thread currentThread();
 * 它是Thread类的一个静态的,本地方法
 *
 * 线程休眠
 * public static native void sleep(long millis) throws InterruptedException;
 * 作用是让当前正在执行的线程休眠xx毫秒(1000ms=1s)
 *
 * Thread类的构造器:
 *      1.无参构造器,这时创建的线程对象,它的名字从"Thread-0","Thread-1"...开始进行排
 *      2.带String参数的 构造器,这个构造器可以给线程名字直接赋值
 *      Thread(String nama)
 *
 * @since 16:29
 * @author wuguidong@cskaoyan.onaliyun.com
 */
public class NameDemo {
    public static void main(String[] args) {
        ThreadA t1 = new ThreadA();
        ThreadA t2 = new ThreadA();
        t1.setName("rap");
        t2.setName("🏀");

        System.out.println(t1.getName());
        System.out.println(t2.getName());
        t1.start();
        t2.start();

        // 获取当前正在执行的线程,也就是主线程
        Thread cThead = Thread.currentThread();
        cThead.setName("鸡杂面");
        System.out.println(cThead.getName());

        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("-------------------");
        ThreadA t3 = new ThreadA("张三");
        t3.start();
        System.out.println(t3.getName());
    }
}

class ThreadA extends Thread {
    public ThreadA() {
        // super();
    }

    public ThreadA(String name) {
        super(name);
    }

    @Override
    public void run() {
        System.out.println("hello world!");
        System.out.println(Thread.currentThread().getName() + "是当前正在执行的线程");
    }
}
